package cn.witsky.rtp2ws.domain;

import cn.witsky.rtp2ws.Server;

public class Monitor {
	//会话id
	private String callId;
	//rtp源地址
	private String source = "";
	//序号
	private int seq;
	//数据量
	private int msgCount;
	private int s1;
	private int s2;
	private int s3;
	private int s4;
	private int s5;
	private int s6;
	private int s7;
	private int s8;
	private int s9;
	//收包总时长
	private int sum;
	//收包平均时长
	private int avg;
	//延迟包个数
	private int delayCount;
	//最大延迟时间
	private int maxDelayTime;
	//最大延迟时间的包序号
	private int maxDelaySeq;	
	//合计延迟时长
	private int maxSumDelayTime;
	//丢包数据量
	private int lostSize;
	//发送数据量
	public int sendMsgCount;
	//发送次数
	public int sendCount;
	//发送成功次数
	public int sendSucCount;
	//发送失败次数
	public int sendErrCount;
	private Long current = null;
	//是主叫还是被叫
	private String type = "";
	//发送最大延迟时长
	private int sendMaxdelayTime;
	//发送延迟次数
	private int sendWsdelayCount;
	//发送最大延迟序号
	private int sendMaxdelaySeq;
	//读取空包合计
	private int readNullSumCount;
	//连续读取空包最大次数
	private int readNullMaxCount;

	private int sumDelayTime = 0;
	
	private long sendWsUseTime=0;

	private int dstPort;

	public Monitor(String callId, boolean isCaller) {
		this.callId = callId;
		if (isCaller) {
			this.type = "caller";
		} else {
			this.type = "called";
		}
	}

	@Override
	public String toString() {
		String str = "";
		int s1Str = Server.getRtpSpeed() ;
		int s2Str = 50;
		int s3Str = 80;
		int s4Str = 100;
		int s5Str = 150;
		int s6Str = 200;
		int s7Str = 250;
		int s8Str = 300;
		int s9Str = 300;
		return str + " 会话id:" + callId + "-" + type + " source:" + source+ " dstPort:" + this.dstPort + " 序号:" + seq + " 消息数:" + msgCount
				+ " 发送消息量:" + sendMsgCount + " 发送次数:" + sendCount + " 发送成功次数:" + sendSucCount + " 发送失败次数:"
				+ sendErrCount + " 丢包量:" + lostSize 
				+ " " + s1Str + "毫秒内:" + s1 
				+ " " + s2Str + "毫秒内:" + s2 
				+ " " + s3Str + "毫秒内:" + s3 
				+ " " + s4Str + "毫秒内:" + s4
				+ " " + s5Str + "毫秒内:" + s5
				+ " " + s6Str + "毫秒内:" + s6
				+ " " + s7Str + "毫秒内:" + s7
				+ " " + s8Str + "毫秒内:" + s8
				+ " 超过" + s9Str + "毫秒:" + s9
				+ " 最大延迟:" + maxDelayTime + " 连续最大延迟:"
				+ maxSumDelayTime + " 最大延迟seq:" + maxDelaySeq + " 延迟量:" + delayCount + " 总耗时:" + sum + " 平均时长:" + avg
				+ " 发送Ws最大延迟:" + sendMaxdelayTime + " 发送Ws获取空包合计:" + readNullSumCount + " 发送Ws获取空包max:"
				+ readNullMaxCount + " 发送Ws最大延迟Seq:" + sendMaxdelaySeq + " 发送Ws延迟次数:" + sendWsdelayCount+ " 发送Ws总耗时:" + sendWsUseTime;
	}

	public Integer setCurrentTime(Long currentTime, int seq) {
		Integer time = 0;
		this.seq = seq;
		if (current == null) {
			current = currentTime;

		} else {
			time = (int) (currentTime - this.current);
			current = currentTime;
		}
		if (time <= Server.getRtpSpeed()) {
			this.s1 = this.s1 + 1;
		} else if (time <=50) {
			this.s2 = this.s2 + 1;
		} else if (time <=80) {
			this.s3 = this.s3 + 1;
		} else if (time <= 100) {
			this.s4 = this.s4 + 1;
		} else if (time <= 150) {
			this.s5 = this.s5 + 1;
		} else if (time <= 200) {
			this.s6 = this.s6 + 1;
		} else if (time <= 250) {
			this.s7 = this.s7 + 1;
		} else if (time <= 300) {
			this.s8 = this.s8 + 1;
		} else {
			this.s9 = this.s9 + 1;
		}
		if (time > Server.getRtpSpeed()) {
			sumDelayTime = sumDelayTime + time;
			this.delayCount = this.delayCount + 1;
			this.setMaxSumDelayTime(sumDelayTime);
		} else {
			sumDelayTime = 0;
		}
		if (maxDelayTime < time) {
			maxDelaySeq = seq;
			maxDelayTime = time;
		}
		lostSize = seq - msgCount;
		this.sum = this.sum + time;
		return time;
	}

	public void setMsgCount(int msgCount) {
		this.msgCount = msgCount;
		this.lostSize = this.seq - this.msgCount;
	}

	public void setAvg() {
		if (this.msgCount > 0) {
			this.avg = this.sum / this.msgCount;
		}
	}

	public int getAvg() {
		return avg;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public int getLostSize() {
		return lostSize;
	}

	public int getSum() {
		return sum;
	}

	public int getSendMsgCount() {
		return sendMsgCount;
	}

	public int getMsgCount() {
		return msgCount;
	}

	public int getDelayCount() {
		return delayCount;
	}

	public void setSendMsgCount(int sendMsgCount) {
		this.sendMsgCount = sendMsgCount;
	}

	public void setSendCount(int sendCount) {
		this.sendCount = sendCount;
	}

	public void setSendSucCount(int sendSucCount) {
		this.sendSucCount = sendSucCount;
	}

	public void setSendErrCount(int sendErrCount) {
		this.sendErrCount = sendErrCount;
	}

	public void setSendMaxdelayTime(int sendMaxdelayTime) {
		this.sendMaxdelayTime = sendMaxdelayTime;
	}

	public String getCallId() {
		return callId;
	}

	public String getType() {
		return type;
	}

	public void setSendWsdelayCount(int sendWsdelayCount) {
		this.sendWsdelayCount = sendWsdelayCount;
	}

	public void setSendMaxdelaySeq(int sendMaxdelaySeq) {
		this.sendMaxdelaySeq = sendMaxdelaySeq;
	}

	public void setMaxSumDelayTime(int maxSumDelayTime) {
		if (this.maxSumDelayTime < maxSumDelayTime) {
			this.maxSumDelayTime = maxSumDelayTime;
		}

	}

	public void setReadNullSumCount(int readNullSumCount) {
		this.readNullSumCount = readNullSumCount;
	}

	public void setReadNullMaxCount(int readNullMaxCount) {
		this.readNullMaxCount = readNullMaxCount;
	}

	public void setSendWsUseTime(long sendWsUseTime) {
		this.sendWsUseTime = sendWsUseTime;
	}

	public void setDstPort(int dstPort) {
		this.dstPort = dstPort;
	}
	
	

}
